﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI;
using Isis.Helpers.Impl;
using Isis.Interfaces;
using Isis.Model;
using Isis.Views;

namespace Isis.Controllers
{
    public class TraceController : ControllerBase
    {
        private readonly string _basePath;

        public TraceController(string basePath)
        {
            _basePath = basePath;
        }

        public override ControllerTab Tab
        {
            get { return new ControllerTab("Trace"); }
        }

        public override void Render(TextWriter writer)
        {
            var pageLayoutWriter = new PageLayoutWriter(Tab.Title, Action, new ControllerRepository(), _basePath);
            pageLayoutWriter.RenderPage(writer, RenderContent);
        }

        private void RenderContent(TextWriter writer)
        {
            RenderTraceInfo(writer);
        }

        // See: http://stackoverflow.com/questions/3856438/redirecting-the-trace-axd-output
        private void RenderTraceInfo(TextWriter writer)
        {
            try
            {
                writer.Write("<h1>Trace Info</h1>");

                var data = TraceAdapter.GetData();

                // display trace requests
                var th = new TableRenderingHelper();

                writer.Write(th.RenderTableHeader("Trace Requests"));
                writer.Write(th.RenderHeaderRow(new[] {"Number", "Time", "Url", "Status", "Verb"}));

                for(int i =0;i<data.Length; i++)
                {
                    DataSet set = (DataSet) data[i];
                    DataTable requestTable = set.Tables["Trace_Request"];
                    DataRow firstRow = requestTable.Rows[0];
                    writer.Write(th.RenderRow(new[]
                                     {
                                         (i + 1).ToString(),
                                         firstRow["Trace_Time_of_Request"].ToString(),
                                         firstRow["Trace_Url"].ToString(),
                                         firstRow["Trace_Status_Code"].ToString(),
                                         firstRow["Trace_Request_Type"].ToString()
                                     }));
                }
                writer.Write("</table>");

               

                //Type trace = HttpContext.Current.Trace.GetType();
                //MethodInfo mi = trace.GetMethod("Render", BindingFlags.Instance | BindingFlags.NonPublic);
                //var sWriter = new StringWriter();
                //var htmlWriter = new HtmlTextWriter(sWriter);
                //var o = new Object[1];
                //o[0] = htmlWriter;
                //mi.Invoke(HttpContext.Current.Trace, o);
                //writer.Write(sWriter.ToString());
            }
            catch (Exception ex)
            {
                writer.Write("<h1>Error accessing Trace</h1>");
                writer.Write("<p>{0}</p>", ex);
            }
        }
    }
}